//
//  ZMEmotionViewCell.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/8/1.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMEmotionViewCell: UICollectionViewCell {
    
    fileprivate lazy var textLabel : UILabel = {
    
        let textlabel = UILabel(text: "", fontSize: 13, textColor: .red)
    
        textlabel.sizeToFit()
               
        return textlabel
    }()
    
    var textString : String?{
        didSet{
            textLabel.text = textString
        }
    }
    //  记录20个表情按钮
    private lazy var emoticonButtonArray: [ZMEmotionViewCellButton] = [ZMEmotionViewCellButton]()

    //记录当前cell 的indexpath
    var indexPath: IndexPath? {
        didSet {
            guard let currentIndexPath = indexPath else {
                return
            }
            
            textString = "当前滚动的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
            
        }
    }

    //显示表情数据源
    var emotionArray: [ZMEmotionModel]?{
        didSet{
            
            //表情数组解包
            guard let currentEmotionArray = emotionArray else{
                return
            }
            
            //默认隐藏所有的表情按钮
            for button in emoticonButtonArray{
                button.isHidden = true
            }
            //遍历表情模型数组
            for (i,emotion) in currentEmotionArray.enumerated() {
                //获取表情按钮
                let button = emoticonButtonArray[i]
                
                button.emotionModel = emotion
                
               
            }
            
        }
    }
    //懒加载删除按钮
    fileprivate lazy var deleteButton: UIButton = {
       let btn = UIButton()
        //添加单击事件
        btn.addTarget(self, action: #selector(deleteButtonClick(sender:)), for: UIControlEvents.touchUpInside)
        btn.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        return btn
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       self.contentView.addSubview(textLabel)
        textLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
        
        setupUI()
    }
    
    func setupUI(){
        addChildButton()
        
    }
    
    //  添加20个表情按钮
    private func addChildButton() {
        //  每项的宽度
        let buttonWidth = width / 7
        //  每项的高度
        let buttonHeight = height / 3
        for i in 0..<20 {
            //  计算列索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            let button = ZMEmotionViewCellButton()
            
            //  设置x，y
            button.x = CGFloat(colIndex) * buttonWidth
            button.y = CGFloat(rowIndex) * buttonHeight
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            //  设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            contentView.addSubview(button)
            
            //给按钮添加单击事件
            button.addTarget(self, action: #selector(emotionButtonClick(sender:)), for: UIControlEvents.touchUpInside)
            
            
            
            //  记录表情按钮
            emoticonButtonArray.append(button)
        }
    }

    //表情按钮单击事件
    @objc private func emotionButtonClick(sender:ZMEmotionViewCellButton){
        
        //获取按钮对应的表情模型
        let emotionModel = sender.emotionModel
        
        ZMEmotiontools.sharedTools.saveRecentEmotionArray(emotion: emotionModel!) { 
            //发送通知,将按钮对应的点击事件和按钮对应的表情模型传递出去
            NotificationCenter.default.post(name: NSNotification.Name(ClickEmoticonButtonNotificationName), object: emotionModel)
            
        }
        
    }
    //MARK:- 删除按钮单击事件
    @objc private func deleteButtonClick(sender: UIButton){
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteEmoticonButtonNotificationName), object: nil)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
